/** 
Copyright (c) 2010, Sheikh Abdul Wahid Ahmed
Details @ http://extremecodeworld.codeplex.com/license
**/

using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Reflection;
using System.IO;
using Microsoft.CSharp;
using System.Text;

namespace EXtremecode.Utils
{
	/// <summary>
	/// Summary description for Util.
	/// </summary>
	public class FileUtil
	{
		

		public static void CopyStreamData(Stream sourceStream,Stream destinationStream)
		{
			try
			{
				sourceStream.Position=0;
				destinationStream.Position=0;

				StreamReader rdr=new StreamReader(sourceStream);
				StreamWriter writer=new StreamWriter(destinationStream);
				writer.AutoFlush=true;
				string line;
				while ((line = rdr.ReadLine()) != null) 
				{
					writer.WriteLine(line);
				}
			}
			catch(Exception ex)
			{
				throw Logging.Logger.Write(ex);
			}
			finally
			{
				//Logging.Logger.Write();
			}

		}

		public static StringBuilder ReadStreamData(Stream stream)
		{
			StreamReader streamReader=new StreamReader(stream);
			StringBuilder sb=new StringBuilder();
			string line;
			while ((line = streamReader.ReadLine()) != null) 
			{
				sb.Append(line + Environment.NewLine);
			}
			
			return sb;
		}

		public static StringBuilder ReadFileData(string filepath)
		{
			if(!File.Exists(filepath))
			{
				throw Logging.Logger.Write(new Exception(
					string.Format("File [{0}] not found",filepath)));
			}
			Stream stream=new FileStream(filepath,FileMode.Open,FileAccess.Read);
			try
			{
				return ReadStreamData(stream);
			}
			finally
			{
				stream.Close();
			}
		
		}

		public static FileInfo CreateFile(string fileName,DirectoryInfo dirInfo)
		{
			FileInfo fileInfo= new FileInfo(dirInfo.FullName + "\\" + fileName);
			fileInfo.Create().Close();
			return fileInfo;
		}
		public static void CreateTextFile(string fileName,string data)
		{
			FileInfo fileInfo= new FileInfo(fileName);
			StreamWriter writer=fileInfo.CreateText();
			writer.Write(data);
			writer.Flush();
			writer.Close();
		}
		public static FileInfo GetFileInfo(string fileName,DirectoryInfo dirInfo)
		{
			FileInfo fileInfo= new FileInfo(dirInfo.FullName + "\\" + fileName);
			return fileInfo;
		}

		public static void CopyFiles(DirectoryInfo sourceDirectory,DirectoryInfo targetDirectory)
		{
			if(sourceDirectory.Exists)
			{
				//first create target dir.
				if(!targetDirectory.Exists)
				{
					targetDirectory.Create();
				}

				//now copy files
				foreach(FileInfo fileInfo in sourceDirectory.GetFiles())
				{
					fileInfo.CopyTo(targetDirectory.FullName + "\\" +  fileInfo.Name,true); 
				}

				//now copy sub directoryies.
				foreach(DirectoryInfo dirInfo in sourceDirectory.GetDirectories())
				{
					CopyFiles(dirInfo,
						new DirectoryInfo(targetDirectory.FullName + "\\" + dirInfo.Name));   
				}

			}
		}

		public static FileInfo ConvertFileInfoInSubfolder(FileInfo fileInfo,string subfolder)
		{
			if(subfolder== null || subfolder.Trim()==string.Empty)
			{
				return fileInfo; 
			}

			FileInfo newFileInfo=new FileInfo(fileInfo.Directory +
				"\\" + subfolder + "\\" + fileInfo.Name);
			
			return newFileInfo;
		}
		
	}
}
